// physical-html
//
// Copyright (c)Daigo Tanaka
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

physHtml.main = function(instanceName){
var private = {};
private.public = this; // need for accessing public method from private

this.physicalObjects = new physHtml.physicalObjects();
this.htmlObjects = new physHtml.htmlObjects();

this.instanceName = instanceName;
this.state = 0; // 1=running, 2:paused, 3:finished, -1:error
this.elapsedTime  = 0;
this.maxTime = 60000;
this.timeInterval = 30;
this.setIntervalID;
this.lockOnTimer = false;
this.console = null;

this.initialize = function(){
  if (this.setIntervalID != null){
    clearInterval( this.setIntervalID );
  }
  this.setIntervalID = null;
  this.instanceName = instanceName;
  this.state = 0;
  this.elapsedTime  = 0;
  this.maxTime = 60000;
  this.timeInterval = 30;
  this.physicalObjects = new physHtml.physicalObjects();
  this.htmlObjects = new physHtml.htmlObjects();
}

this.start = function()
{
  this.state = 1; // started
  if (typeof window[this.instanceName] == undefined){
    throw "Could not find PhysicalHtml instance!";
    this.state = -1;
  }
  $functionString = this.instanceName + ".onTimer()";
  this.setIntervalID = setInterval( $functionString, this.timeInterval);
}

this.stop = function()
{
  clearInterval( this.setIntervalID );
  this.state = 2;
}

this.onTimer = function()
{
  if (this.lockOnTimer == true){
    return;
  }

//  for (var j = 0; j < this.physicalObjects.objects.length; j++){
//    PhysicalHtmlDebugMessage += this.physicalObjects.objects[j].width.toFixed(0) + "px ";
//  }

  this.lockOnTimer = true;
  this.elapsedTime += this.timeInterval;

  // Check the finish time first before any potential coding error kicks in.
  if (this.maxTime > 0 && this.elapsedTime > this.maxTime){
    this.stop();
    this.state = 3;
  }
  this.physicalObjects.update(this.elapsedTime);
  this.htmlObjects.render();
  if (physHtml.PhysicalHtmlDebugMessage != "" && this.console != null){
    this.console.innerHTML = PhysicalHtmlDebugMessage;
  }

  this.lockOnTimer = false;
}

this.getTime = function()
{
  return elapsedTime;
}

}


